package cn.ycxy.lbl.repairs.controller;

import cn.ycxy.lbl.common.exception.BusinessException;
import cn.ycxy.lbl.common.exception.enums.ResponseEnum;
import cn.ycxy.lbl.common.restful.JsonPage;
import cn.ycxy.lbl.common.restful.R;
import cn.ycxy.lbl.pojo.repairs.domain.LblCsMaintainInfo;
import cn.ycxy.lbl.pojo.repairs.dto.MaintainInfoListDTO;
import cn.ycxy.lbl.pojo.repairs.dto.MaintainInfoUpdateDTO;
import cn.ycxy.lbl.repairs.service.IMaintainInfoService;
import cn.ycxy.lbl.repairs.utils.SecurityContextUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/maintain")
public class MaintainInfoController {

    @Autowired
    private IMaintainInfoService maintainInfoService;


    @PreAuthorize("hasRole('user')")
    @GetMapping("/list")
    public R<JsonPage<LblCsMaintainInfo>> getList(@Validated MaintainInfoListDTO dto){
        Long userId = SecurityContextUtils.getUserId();
        QueryWrapper<LblCsMaintainInfo> wrapper = new QueryWrapper<>();
        wrapper.select("id", "question_detailed", "status", "create_time");
        wrapper.eq("user_id", userId);
        if (dto.getStatus() != null && dto.getStatus() != -1){
            wrapper.eq("status", dto.getStatus());
        }else {
            wrapper.ne("status", 4);
        }
        Page<LblCsMaintainInfo> pageParam = new Page<>(dto.getPageNo(), dto.getPageSize());
        maintainInfoService.page(pageParam, wrapper);
        JsonPage<LblCsMaintainInfo> jsonPage = new JsonPage<>();
        jsonPage.setPage(dto.getPageNo());
        jsonPage.setPageSize(dto.getPageSize());
        jsonPage.setList(pageParam.getRecords());
        jsonPage.setTotal(pageParam.getTotal());
        return R.ok(jsonPage);
    }

    @PreAuthorize("hasRole('user')")
    @GetMapping("/detail/{id}")
    public R<LblCsMaintainInfo> getDetails(@PathVariable("id") Integer id){
        LblCsMaintainInfo info = maintainInfoService.getById(id);
        return R.ok(info);
    }

    @PreAuthorize("hasRole('user')")
    @PostMapping("/modify")
    public R<Void> update(@Validated @RequestBody MaintainInfoUpdateDTO updateDTO){
        LblCsMaintainInfo maintainInfo = maintainInfoService.getById(updateDTO.getId());
        if (maintainInfo.getStatus() == 1 || maintainInfo.getStatus() == 2){
            throw new BusinessException(ResponseEnum.MAINTAIN_UPDATE_ERROR);
        }
        BeanUtils.copyProperties(updateDTO, maintainInfo);
        if (!maintainInfoService.updateById(maintainInfo)){
            throw new BusinessException(ResponseEnum.UPDATE_ERROR);
        }
        return R.ok();
    }

    @PreAuthorize("hasRole('user')")
    @PostMapping("/confirm")
    public R<Void> add(@RequestBody @Validated LblCsMaintainInfo maintainInfo){
        Long userId = SecurityContextUtils.getUserId();
        maintainInfo.setUserId(userId);
        if (!maintainInfoService.save(maintainInfo)) {
            throw new BusinessException(ResponseEnum.MAINTAIN_SAVE_FAILED);
        }
        return R.ok();
    }




}
